package com.guosifan.controller;

import com.guosifan.entity.*;
import com.guosifan.feign.OrderClientFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/client/order/")
public class OrderClientController {

    @Autowired
    private OrderClientFeign orderClientFeign;

    @PostMapping("/save/{id}")
    public Msg save(@PathVariable("id")int id, HttpSession httpSession){
        User user = (User)httpSession.getAttribute("user");
        Order order = new Order();
        order.setUser(user);
        order.setMenu(new Menu().setId(id));
        order.setDate(new Date());
        orderClientFeign.save(order);
        return Msg.success();
    }

    @GetMapping("/findByUid")
    public AllVO findByUid(@RequestParam("page")int page,@RequestParam("limit")int limit,HttpSession httpSession){
        User user = (User)httpSession.getAttribute("user");
        int count = orderClientFeign.findByIdCount(user.getId());
        List<Order> orders = orderClientFeign.findByUid(user.getId(), (page - 1) * limit, limit);
        return new AllVO(0,"",count,orders);
    }

    @GetMapping("/findByState")
    public AllVO findByState(@RequestParam("page")int page ,@RequestParam("limit")int limit){
        int count=orderClientFeign.count();
        List<Order> orders=orderClientFeign.findByState((page-1)*limit,limit);
        return new AllVO(0,"",count,orders);
    }

    @GetMapping("/count")
    public int count(){
        return orderClientFeign.count();
    }

    @GetMapping("/updateState/{id}")
    public Msg updateState(@PathVariable("id")int id){
        orderClientFeign.updateState(id);
        return Msg.success();
    }





}
